<?php

require 'db.php';
//新闻类型分类
$news_type=0;

$strsql0 = "select * from newstype";
$result0 = mysqli_query($link, $strsql0);
$data_type = mysqli_fetch_all($result0);

$strsql = "select * from newspaper left join newstype on newspaper.news_type = newstype.Id";
if(!empty($_GET['search'])){
    $strsql  .= " where news_title like '%".$_GET['search']."%'"; //模糊匹配
}
else if(isset($_GET['type']) || isset($_GET['date'])){
    $news_type = $_GET['type'];
    $news_date = $_GET['date'];
        if($news_type != "0" && $news_date !=""){
            /*
            echo "<script>alert('警告：【新闻类别】和【新闻时间】不能同时选择！！！')</script>";
            echo "<script>window.history.go(-1);</script>";
            exit();*/
            $strsql .=" where news_type = '".$news_type."' and news_time = '".$news_date."' ";
        }
        else if ($news_date !="" && $news_type =="0"){
            $strsql .=" where news_time = '".$news_date."'";
        }
        else if ($news_type !="0" && $news_date ==""){
            $strsql .=" where news_type = '".$news_type."'";
        }
        
}
/*
else{
    echo "<script>alert('数据查询错误！')</script>";
    echo "<script>window.history.go(-1);</script>";
    exit();
}*/

$result = mysqli_query($link, $strsql);
$data = mysqli_fetch_all($result);

mysqli_free_result($result);
mysqli_close($link);


$per_page = 10;//每页显示的条数
$total_num = count($data);//总条数
$total_page = ceil($total_num / $per_page);//向上取整

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;//获取当前页码
//页码合理性判断的第二种方式，两种方式任选其一即可

$page = max($page,1);
$page = min($page,$total_page);

//页码合理性判断的第二种方式，任选其一即可
$pre_page = ($page - 1) < 1 ? $page : ($page-1);
$next_page =($page + 1) > $total_page ? $page : ($page+1);
// $pre_page = $page - 1;
// $next_page =$page + 1;
$page_1=$page+1;
$page_2=$page+2;
$page_3=$page+3;

$end_index = $per_page*$page - 1;
$end_index = min($end_index,$total_num - 1);

$start_index = ($per_page*($page - 1)-1)+1;

require 'index.html';
?>